﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DbMapping.Common;
using VideoCache.Server.BLL;
using VideoCache.Server.Model;
using VideoCache.Server.MVCWeb.Areas.Admin.Models;
using VideoCache.Server.MVCWeb.Attributes;
using VideoCache.Aggregation;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Controllers
{
    [ActionAuthorize(AuthorizeType.Administrator)]
    public class EpisodeController : Controller
    {
        public ActionResult Index(int id)
        {
            AlbumSiteModel model = new AlbumSiteModel(id);

            if (model == null) return HttpNotFound();

            return View(model);
        }

        public void Delete(int id)
        {
            EpisodeBLL.Delete(id);
        }

        [HttpPost]
        public ActionResult Delete(string ids)
        {
            List<int> list = new List<int>();
            foreach (string str in ids.Split(','))
            {
                int id;
                if (int.TryParse(str, out id))
                {
                    list.Add(id);
                }
            }
            EpisodeBLL.Delete(list.ToArray());

            return Content(null);
        }

        [HttpPost]
        public ActionResult SetDownload(string ids)
        {
            List<int> list = new List<int>();
            foreach (string str in ids.Split(','))
            {
                int id;
                if (int.TryParse(str, out id))
                {
                    list.Add(id);
                }
            }
            EpisodeBLL.SetDownload(list.ToArray());

            return Content(null);
        }

        public ActionResult ClearEpisode(int albumId)
        {
            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.AlbumIdField, CompareType.Equal, albumId));

            EpisodeBLL.Delete(cons);

            return RedirectToAction("Index", new { id = albumId });
        }

        [HttpPost]
        public ActionResult UpdateEpisodes(int albumId)
        {
            try
            {
                EpisodeBLL.SearchEpisodes(albumId);
                return Json(null);
            }
            catch (Exception ex)
            {
                return Json(null);
            }
        }

        public ActionResult Create(int albumId, int siteId = 0)
        {
            ViewBag.AlbumId = albumId;
            ViewBag.SiteId = new SelectList(SiteBLL.FindAll(null, null), "Id", "DisplayName", siteId);

            return View();
        }

        public ActionResult Edit(int id)
        {
            EpisodeModel model = new EpisodeModel(id);

            ViewBag.AlbumId = model.AlbumId;
            ViewBag.SiteId = new SelectList(SiteBLL.FindAll(null, null), "Id", "DisplayName", model.SiteId);

            return View(model);
        }

        public ActionResult FindToken(int id)
        {
            EpisodeInfo info = EpisodeBLL.Find(id);

            if (info == null) return HttpNotFound();

            string url = info.Url;

            VideoModel model = FetcherFactory.GetToken(url);

            if (model != null)
            {
                info.Token = model.Token;
                info.Name = model.VideoName;
                info.ModifyTime = DateTime.Now;
                EpisodeBLL.Update(info);
            }

            return RedirectToAction("Index", info.AlbumId);
        }

        [HttpPost]
        public ActionResult FindToken(string ids)
        {
            foreach (string item in ids.Split(','))
            {
                int id;
                if (int.TryParse(item, out id))
                {
                    this.FindToken(id);
                }
            }

            return Json(null);
        }

        public ActionResult FindAllToken(int albumId)
        {
            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.AlbumIdField, CompareType.Equal, albumId));

            var list = EpisodeBLL.FindAll(cons, null);

            foreach (EpisodeInfo info in list)
            {
                if (string.IsNullOrEmpty(info.Token))
                {
                    this.FindToken(info.Id);
                }
            }

            return RedirectToAction("Index", new { id = albumId });
        }
    }
}